package com.elem.dao.impl;

import com.elem.bean.Food;
import com.elem.dao.FoodDao;
import com.elem.util.DruidUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    // 创建JdbcTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
        String sql = "select * from food where businessId = ?";
        List<Food> foodList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Food.class), businessId);
        return foodList;
    }

    @Override
    public Food getFoodById(Integer foodId) throws Exception {
        try {
            String sql = "select * from food where foodId = ?";
            Food food = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Food.class), foodId);
            return food;
        } catch (DataAccessException e) {
            return null;
        }
    }
}
